package z_mxsTest.testServiceConfig;

import ma.xin.shi.configurations.Service.*;

public class TestServiceConfig {

    public static void main(String[] args) {
        // 创建服务配置管理器
        ServiceConfig serviceConfig = new ServiceConfig();

        // === 添加服务（仅名称和ID）===
        System.out.println("=== 添加服务（仅名称和ID）===");
        serviceConfig.addServiceEx("TemperatureSensor", 0x11);
        System.out.println("✅ 已添加服务: TemperatureSensor (ID: 0x11)");

        serviceConfig.addServiceEx("CoolingSystem", 0x22);
        System.out.println("✅ 已添加服务: CoolingSystem (ID: 0x22)");

        // === 查询所有服务 ===
        System.out.println("\n=== 查询所有服务 ===");
        for (ServiceEntry entry : serviceConfig.getAllServices()) {
            System.out.println("服务名: " + entry.getServiceName() +
                               ", ID: 0x" + Integer.toHexString(entry.getServiceId()).toUpperCase());
            System.out.println("  Events: " + entry.getEvents().size());
            System.out.println("  Fields: " + entry.getFields().size());
            System.out.println("  Methods: " + entry.getMethods().size());
        }

        // === 添加事件到服务 ===
        System.out.println("\n=== 添加事件到服务 ===");
        ServiceEventConfig eventA = new ServiceEventConfig();
        eventA.setEventName("HighTempAlert");
        eventA.setRPCSpecificType("temp > 100°C");

        serviceConfig.addEventToService("TemperatureSensor", eventA);
        System.out.println("✅ 已向 TemperatureSensor 添加事件: HighTempAlert");

        // === 添加字段到服务 ===
        System.out.println("\n=== 添加字段到服务 ===");
        ServiceFieldConfig fieldB = new ServiceFieldConfig();
        fieldB.setFieldName("CurrentCoolingLevel");
        fieldB.setRPCSpecificType("UINT8");
        fieldB.setRPCProtocol("0");

        serviceConfig.addFieldToService("CoolingSystem", fieldB);
        System.out.println("✅ 已向 CoolingSystem 添加字段: CurrentCoolingLevel");

        // === 添加方法到服务 ===
        System.out.println("\n=== 添加方法到服务 ===");
        ServiceMethodConfig methodA = new ServiceMethodConfig();
        methodA.setMethodName("GetTemperature");

        serviceConfig.addMethodToService("TemperatureSensor", methodA);
        System.out.println("✅ 已向 TemperatureSensor 添加方法: GetTemperature");

        // 再次查询所有服务
        System.out.println("\n=== 当前所有服务详情 ===");
        for (ServiceEntry entry : serviceConfig.getAllServices()) {
            System.out.println("服务名: " + entry.getServiceName() +
                               ", ID: 0x" + Integer.toHexString(entry.getServiceId()).toUpperCase());

            for (ServiceEventConfig e : entry.getEvents()) {
                System.out.println("  Event: " + e.getEventName());
            }
            for (ServiceFieldConfig f : entry.getFields()) {
                System.out.println("  Field: " + f.getFieldName());
            }
            for (ServiceMethodConfig m : entry.getMethods()) {
                System.out.println("  Method: " + m.getMethodName());
            }
        }

        // === 更新服务 ===
        System.out.println("\n=== 更新服务 ===");
        ServiceEntry updatedService = new ServiceEntry();
        updatedService.setServiceName("TemperatureSensorV2");
        updatedService.setServiceId(0x11); // ID 保持不变

        boolean isUpdated = serviceConfig.updateService("TemperatureSensor", updatedService);
        if (isUpdated) {
            System.out.println("✅ 服务已更新为: " + updatedService.getServiceName());
        } else {
            System.out.println("❌ 服务更新失败");
        }

        // === 删除服务 ===
        System.out.println("\n=== 删除服务 ===");
        boolean isRemoved = serviceConfig.removeService("CoolingSystem");
        if (isRemoved) {
            System.out.println("✅ 服务已删除: CoolingSystem");
        } else {
            System.out.println("❌ 服务删除失败");
        }

        // 最终查询所有服务
        System.out.println("\n=== 最终查询所有服务 ===");
        for (ServiceEntry entry : serviceConfig.getAllServices()) {
            System.out.println("服务名: " + entry.getServiceName() +
                               ", ID: 0x" + Integer.toHexString(entry.getServiceId()).toUpperCase());
        }
    }
}
